<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, 
							maximum-scale=1.0, minimum-scale=1.0, 
							user-scalable=no" />
<title>Hello World</title>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="js/phonegap-nfc.js"></script>
<!-- <script src="http://70.12.110.130:7777/target/target-script-min.js"></script> -->



<script type="text/javascript">
	//ReadMode, WriteMode
	var currentMode = "ReadMode";

	$(function() {
		showCurMode(currentMode);
		$("#tagReadBut").on("click", function() {
			currentMode = "ReadMode";
			showCurMode(currentMode);
		});
		$("#tagWriteBut").on("click", function() {
			currentMode = "WriteMode";
			showCurMode(currentMode);
		});
		//console.log("index.html ========= 26 called...")
		document.addEventListener('deviceready', onDeviceReady, false);
	})

	function showCurMode(curMode) {
		console.log("=========== showCurMode call....");
		$("#currentModeInfo").html("<mark>" + curMode + "</mark>");
	}

	function onDeviceReady() {
		alert("onDeviceReady");
		enableTagReadWrite();
	}

	function enableTagReadWrite() {

		console.log("============== enableTagReadWrite");

		nfc.addNdefListener(function(nfcEvent) {

			if (currentMode == "ReadMode") {
				tagRead(nfcEvent);
			} else if (currentMode == "WriteMode") {
				//tagWrite(nfcEvent);
				tagWriteUri(nfcEvent);
			} else {

			}

		}, function() {//success callback
			if (currentMode == "ReadMode") {
				alert("ReadMode : Waiting for Ndef Tag...");
			} else if (currentMode == "WriteMode") {
				alert("WriteMode : Waiting for Write Tag...");
			}
		}, function(error) {//error callback
			alert("Error adding Ndef Listener" + JSON.stringigy(error));
		});

	}

	function tagRead(nfcEvent) {

		alert("ReadMode tag scanned...");
		var tag = nfcEvent.tag;
		//alert(tag);
		var ndefMsg = tag.ndefMessage;
		//alert(JSON.stringify(ndefMsg));
		console.log("ndefMsg....");
		console.log(ndefMsg);

		var result = "";

		alert(ndefMsg[0].type);

		if (ndefMsg[0].type == ndef.RTD_TEXT[0]) {
			alert("text type");
			//result = nfc.bytesToString(ndefMsg[0].payload).substring(3);
			result = ndef.textHelper.decodePayload(ndefMsg[0].payload)

		} else if (ndefMsg[0].type == ndef.RTD_URI[0]) {
			//alert("uri type");
			result = ndef.uriHelper.decodePayload(ndefMsg[0].payload);
			if (result.match("http")) {
				alert(result + " 이동할께...")
				location.href = result;
			}
		}

		//alert(nfc.bytesToString(ndefMsg[0].payload).substring(3));
		$("#readMsg").html(result);
		//$("#tagConvertContent").html();
		//alert()

	}

	function tagWriteUri() {

		alert("Write Uri Mode tag detected....");
		var tagWriteMsg = $("#writeMsg").val();
		alert("write msg ==" + tagWriteMsg);
		//textRecord: function (text, languageCode, id) {
		var uriRecord = ndef.uriRecord(tagWriteMsg);
		var message = [ uriRecord ];

		nfc.write(message, function() {
			navigator.notification.beep(1);
			navigator.notification.alert(message);
		}, function(reason) {
			navigator.notification.alert(reason, function() {
			}, "다시 탭해주세요.");
		});

	}

	function tagWrite() {

		alert("Write Mode tag detected....");
		var tagWriteMsg = $("#writeMsg").val();
		alert("write msg ==" + tagWriteMsg);
		//textRecord: function (text, languageCode, id) {
		var textRecord = ndef.textRecord(tagWriteMsg, "utf-8");
		var message = [ textRecord ];

		nfc.write(message, function() {
			navigator.notification.beep(1);
			navigator.notification.alert(message);
		}, function(reason) {
			navigator.notification.alert(reason, function() {
			}, "다시 탭해주세요.");
		});

	}
</script>
</head>
<body>
	<h1 id=currentModeInfo>읽기모드</h1>
	<div id="nfcRead">
		<h1>MyNfcReader</h1>
		<h3>읽은 Tag Data</h3>
		<textarea name="payload" id="readMsg" rows="5" cols="30"></textarea>
		<br /> <input type="button" name="tagRead" id="tagReadBut"
			value="tagRead" />
	</div>
	<div id="nfcWrite">
		<h1>MyNfcWrite</h1>
		<h3>Tag에 작성할 내용</h3>
		<textarea name="payload" id="writeMsg" rows="5" cols="30">http://www.naver.com</textarea>
		<br /> <input type="button" name="tagWrite" id="tagWriteBut"
			value="tagWrite" />
	</div>
</body>
</html>
